<?php
/**
 * Created by PhpStorm.
 * User:tsingStar
 * Date: 2020/6/16
 * Time: 13:34
 */

namespace app\user\controller;


use app\user\model\WeixinPay;
use app\user\model\Zfb;
use think\Controller;
use think\Db;
use think\Log;
use think\Request;

require_once EXTEND_PATH . 'wechat/WxPayConfig.php';
include EXTEND_PATH . "alipay/AopSdk.php";

class Payresult extends Controller
{

    /**
     * 微信支付回调入口
     */
    public function wx()
    {
        $wxpay_notify = new WeixinPay();
        $config = new \WxPayConfig();
        $wxpay_notify->Handle($config);
    }

    /*支付宝回调*/
    public function ali()
    {
        try {
            \db()->startTrans();
            /*获取全部返回结果*/
            if (!empty($_POST)) {
                if ($_POST['trade_status'] != 'TRADE_SUCCESS') {
                    echo "fail";
                    die;
                } else {
                    $order_sn = $_POST['out_trade_no'];
                    $order_info = db('shop_dingdan')
                        ->where('danhao', $order_sn)
                        ->find();
                    if ($order_info['pay_ok'] == 1) {
                        echo "success";
                        die;
                    }

                    if (empty($order_info) || bccomp($order_info['xiaofei'], $_POST['total_amount'], 2) != 0) {
                        echo "fail";
                        die;
                    } else {
                        $aop = Zfb::getAop();
                        /*验签*/
                        $verify = $aop->rsaCheckV1($_POST, '', 'RSA2');

                        if (!$verify) {
                            echo "fail";
                            die;
                        }
                        //保存支付状态
                        $change = [
                            'pay_ok' => 1,
                            'is_ok' => 2,
                            'pay_type' => 'zfb',
                            'pay_sn' => $_POST['trade_no'],
                            'pay_money' => $_POST['total_amount'],
                            'pay_time' => time()
                        ];
                        db('shop_dingdan')->where('id', $order_info['id'])->update($change);
                        //减少库存
                        db('shop')->where('id='.$order_info['shop_id'])->setDec('kucun',$order_info['num']);
                        //删除购物车

                        db('shop_car')->where('shop_id', $order_info['shop_id'])->where('userid', $order_info['userid'])->delete();
                        $cp=db('shop')->where('id', $order_info['shop_id'])->find();
                        if($cp['catid']==6){
                            $pv=$cp['pv'];
                            jihuo($order_info['userid'],$pv);
                            daili($order_info['userid'],$order_info['shop_id']);
                        }

                        echo "success";
                    }
                }
            } else {
                echo "fail";
            }
            \db()->commit();
        } catch (\Exception $e) {
            \db()->rollback();
        }

    }
}